package com.hu._05_topic;

import com.hu.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

//消息接受者
public class Rev2 {

    //消费者2的专属队列
    public static final String NAME_QEUEUE2_TOPIC = "name_qeueue222_topic";

    public static void main(String[] args) throws Exception {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(NAME_QEUEUE2_TOPIC,true,false,false,null);
        //绑定到交换机
        channel.queueBind(NAME_QEUEUE2_TOPIC, Send.NAME_EXCHANGE_TOPIC,"user.*");

        //同时处理一个消息
        channel.basicQos(1);

        Consumer consumer = new  DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //int  i = 1 / 0 ;    //模拟消息消费中断
                System.out.println("消费者的ID："+consumerTag);
                System.out.println("消息的ID："+envelope.getDeliveryTag());
                System.out.println("交换机："+envelope.getExchange());
                System.out.println("队列："+envelope.getRoutingKey());
                System.out.println("收到消息："+new String(body));
                System.out.println("消息消费成功....................................");
                //手动签收消息
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };
        channel.basicConsume(NAME_QEUEUE2_TOPIC,false,consumer);
    }
}
